/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useState, useEffect } from 'react';
import { Form, Button, message } from 'antd';
import { getTemplateAuditList, smsSignchangeStatus } from '@/services/templateAudit';
import TemCommonTable from './common/TemCommonTable';
import type { CommonIndexType } from '../types/Common';

import { formatFormValue, formatSignApproveParams, SignApproveColumns } from './utils';
import TemCommonForm from './common/TemCommonForm';

const SignReject = (props: CommonIndexType) => {
  const { screenSize, pageKey, show } = props;
  const [form] = Form.useForm();
  const [dataList, setDataList] = useState([]);
  const [pagination, setPagination] = useState({});

  const onFinish = async ({ current }: any) => {
    const formValues = formatFormValue(form.getFieldsValue());

    const { dataList: list, pagination: paginationParams } = await getTemplateAuditList({
      page_num: current || 1,
      part: pageKey,
      state: 'reject',
      ...formValues,
    });

    setDataList(list);
    setPagination({ ...paginationParams });
  };

  // 每次展示重新请求列表
  useEffect(() => {
    if (show) {
      onFinish({});
    }
  }, [show]);

  const onOperation = async (ids: any) => {
    const { result } = await smsSignchangeStatus({
      state: 'approved',
      ...formatSignApproveParams([ids]),
    });
    if (result) {
      message.info('操作成功');
      onFinish(pagination);
    }
  };

  const oparateCoumn = {
    title: '操作',
    dataIndex: 'user',
    render: (_: any, record: any) => (
      <Button type='primary' onClick={onOperation.bind(null, record)}>
        通过审核
      </Button>
    ),
  };

  return (
    <>
      <TemCommonForm form={form} screenSize={screenSize} onFinish={onFinish} isShowDate />
      <TemCommonTable
        rowKey='kb_id'
        columns={[...SignApproveColumns, oparateCoumn]}
        dataSource={dataList}
        onChange={onFinish}
        pagination={{
          simple: screenSize,
          ...pagination,
        }}
      />
    </>
  );
};

export default SignReject;
